<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="模型代号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="modelKey">
              <a-input v-model="model.modelKey" placeholder="请输入模型代号"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="模型名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="modelName">
              <a-input v-model="model.modelName" placeholder="请输入模型代号"  ></a-input>
            </a-form-model-item>
          </a-col>

           <a-col :span="12">
            <a-form-model-item label="业务代号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessKey">
              <a-input v-model="model.businessKey" placeholder="请输入业务代号"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="业务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessName">
              <a-input v-model="model.businessName" placeholder="请输入业务名称"  ></a-input>
            </a-form-model-item>
          </a-col>

     
         

          <a-col :span="12">
           
            <a-form-model-item label="流程分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId"> 
              <el-select @change="handleCategoryChange" v-model="model.categoryId" placeholder="请选择流程分类" clearable prop="categoryId">
              <el-option label="请选择" value="" />
              <el-option v-for="category in  categorys" :key="category.id" :label="category.name" :value="category.id" />
            </el-select>  

             <el-input
              type="hidden"
              name="categoryName"
              v-model="model.categoryName"
              style="display:none"
            >
            </el-input>
            
            </a-form-model-item>
          </a-col>


<!-- 
          <a-col :span="12">
            <a-form-model-item label="模型状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useStatus">
              <el-select   v-model="model.useStatus" placeholder="请选择流程分类" clearable prop="useStatus">
              <el-option label="请选择" value="" />
              <el-option label="待发布" value="1" />
              <el-option label="启用" value="2" />
              <el-option label="停用" value="-1" /> 

              
              
            </el-select>
            </a-form-model-item>
          </a-col> -->


          
          <a-col :span="12">

            
          
          </a-col>
 
          <!-- <a-col :span="12">
            <a-form-model-item label="前端显示状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="frontShow">
              <j-multi-select-tag type="checkbox" placeholder="请选择前端显示状态" v-model="model.frontShow"  style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          
           
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
import {
  categoryList 
} from "@views/flowable/api/definition";

  export default {
    name: 'TestDemoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        categorys: [],
        // useStatusList:[
        //   {label:'待发布',value:'1'},
        //   {label:'启用',value:'2'},
        //   {label:'停用',value:'-1'},
        // ],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入用户名!'},
           ],
        },
        url: {
          add: "/flow/model",
          edit: "/flow/model",
          queryById: "/flow/category"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.init();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      init(){
         
        categoryList({}).then(res=>{
          this.categorys = res.result||[]
        })
      },
      handleCategoryChange(val){ 
         
        var obj = {}
        obj = this.categorys.find(function(item){
        return item.id === val
        }) 
        this.model.categoryName = obj.name 
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success("保存数据成功");
                that.$emit('ok');
              }else{
                that.$message.warning("保存数据失败!"+res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>